package eScrum.pages;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import eScrum.HomePage;
import eScrum.UserSession;
import eScrum.components.MWindow;
import eScrum.panels.ChangePasswordPopUp;


public class TeleBasePage  extends BasePage {

	private static final long serialVersionUID = 1L;

	public TeleBasePage() {
		super();
	}

	public TeleBasePage(IModel<?> model) {
		super(model);
	}

	public TeleBasePage(PageParameters parameters) {
		super(parameters);
	}

	@Override
	public void init() {
		final UserSession session = (UserSession) getSession();

		final MWindow changePasswordWindow = new MWindow("modalChangePasswordPage");
		changePasswordWindow.setTitle(getString("changePassword"));
		changePasswordWindow.setInitialHeight(250);
		changePasswordWindow.setInitialWidth(480);
		changePasswordWindow.setResizable(false);
		
		add(changePasswordWindow);

		final UserSession userSession = (UserSession) getSession();
		add(new AjaxLink<String>("changePassLink") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				changePasswordWindow.setContent(new ChangePasswordPopUp(changePasswordWindow.getContentId(), userSession.getUser().getId()) {
							private static final long serialVersionUID = 1L;

							@Override
							public void refreshData(AjaxRequestTarget target) {
								super.refreshData(target);
							}

						});
				changePasswordWindow.show(target);
			}
		});
		add(new Label("name", new PropertyModel<String>(userSession.getUser(), "username")));

		add(new Link<Object>("logout") {
			private static final long serialVersionUID = 1L;

			public void onClick() {
				try {
					session.invalidate();
					setResponsePage(HomePage.class);
				} catch (Exception e) {
					reportUnexpectedException(e);
				}
			}
		});
	}

	protected void reportUnexpectedException(Exception e) {
		e.printStackTrace();
		error(getString("unexpectedError") + (e.getMessage() == null ? getString("unknown") : e.getMessage()));
	}

}
